<template>
	<view class="content">
		<view class="row b-b">
			<text class="tit">联系人</text>
			<input class="input" type="text" v-model="addressData.person" placeholder="收货人姓名"
				placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">手机号</text>
			<input class="input" type="number" v-model="addressData.phone" placeholder="收货人手机号码"
				placeholder-class="placeholder" />
		</view>
		<view class="row b-b" >
			<text class="tit">地区</text>
			<!-- #ifdef H5 -->
			<cityPicker :column="3" :default-value="defaultRegion" :mask-close-able="true"
				@confirm="chooseArea" @cancel="areaChooseVisible = false" :visible="areaChooseVisible" />
			<view @tap="areaChooseVisible = true" style="width: 100%;">{{addressData.area ? addressData.area : '点击选择地区'}}</view>
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
			<cityPicker :column="3" :default-value="defaultRegion" :mask-close-able="true"
				@confirm="chooseArea" @cancel="areaChooseVisible = false" :visible="areaChooseVisible" />
			<view @tap="areaChooseVisible = true" style="width: 100%;">{{addressData.area ? addressData.area : '点击选择地区'}}</view>
			<!-- #endif -->
		</view>
		<view class="row b-b">
			<text class="tit">门牌号</text>
			<input class="input" type="text" v-model="addressData.address" placeholder="楼号、门牌"
				placeholder-class="placeholder" />
		</view>

		<view class="row default-row">
			<text class="tit">标签</text>
			<view class="normal-tag" :class="addressData.tag === '家' ? 'choose-tag' : ''" @tap="chooseTag('家')">家</view>
			<view class="normal-tag" :class="addressData.tag === '公司' ? 'choose-tag' : ''" @tap="chooseTag('公司')">公司
			</view>
			<view class="normal-tag" :class="addressData.tag === '学校' ? 'choose-tag' : ''" @tap="chooseTag('学校')">学校
			</view>
		</view>

		<view class="row default-row">
			<text class="tit">设为默认</text>
			<tn-switch v-model="addressData.defaultAddress" activeColor="#fa436a"></tn-switch>
		</view>
		<button class="add-btn" @click="confirm">提交</button>
	</view>
</template>

<script>
	import cityPicker from '@/uni_modules/piaoyi-cityPicker/components/piaoyi-cityPicker/piaoyi-cityPicker'
	export default {
		components: {
			cityPicker
		},
		data() {
			return {
				defaule: false,
				areaChooseVisible: false,

				addressData: {
					"person": "",
					"phone": "",
					"area": "",
					"address": "",
					"tag": "家",
					"defaultAddress": false,
				},
				defaultRegion: ['湖北省', '武汉市', '洪山区']
			}
		},
		onLoad(option) {
			let title = '新增收货地址';
			if (option.type === 'edit') {
				title = '编辑收货地址'
				this.addressData = JSON.parse(option.data)
				if (this.addressData.defaultAddress == 1) {
					this.defaule = true
				}
			}
			this.manageType = option.type;
			uni.setNavigationBarTitle({
				title
			})
		},

		methods: {
			switchChange(e) {
				this.default = e.detail;
				this.addressData.defaultAddress = e.detail;
			},
			chooseTag(e) {
				this.addressData.tag = e;
			},
			// 省市区获取
			chooseArea(val) {
				console.log("选中的结果:" + val)
				this.areaChooseVisible = false
				let pname = val['provinceName'] + '/' +  val['cityName'] + '/' + val['areaName']
				this.addressData.area = pname;
			},
			//提交
			confirm() {
				let data = this.addressData;
				if (!data.person) {
					this.$api.msg('请填写收货人姓名');
					return;
				}
				if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(data.phone)) {
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if (!data.address) {
					this.$api.msg('请填写收货地址');
					return;
				}
				
				if (data.area == '点击选择地区' || !data.area) {
					this.$api.msg('请选择省市区');
					return;
				}

				let insert = data.addressId && data.addressId > 0;
				let _this = this;
				this.$Net.req({
					url: insert ? this.$API.execUpdateUserAddress() : this.$API.execAddUserAddress(),
					method: "post",
					header: {
						'Content-Type': 'application/json'
					},
					data: data,
					dataType: 'json',
					success: function(result, status) {
						if (status.code == 0) {
							_this.goBack();
						} else {
							_this.$api.msg((insert ? '新增失败:' : '修改失败:') + status.msg);
						}
					},
				})
			},
			goBack() {
				// 通过判断当前页面的页面栈信息，是否有上一页进行返回，如果没有则跳转到首页
				const pages = getCurrentPages()
				console.log('返回上一级 -->', pages);
				if (pages && pages.length > 0) {
					const firstPage = pages[0]
					if (pages.length == 1 && (!firstPage.route || firstPage.route != '/pages/address/address')) {
						uni.reLaunch({
							url: '/pages/address/address'
						})
					} else {
						uni.navigateBack({
							delta: 1
						})
					}
				} else {
					uni.reLaunch({
						url: '/pages/address/address'
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-top: 16upx;
	}

	.row {
		display: flex;
		align-items: center;
		position: relative;
		padding: 0 30upx;
		height: 110upx;
		background: #fff;

		.tit {
			flex-shrink: 0;
			width: 120upx;
			font-size: 30upx;
			color: $font-color-dark;
		}

		.input {
			flex: 1;
			font-size: 30upx;
			color: $font-color-dark;
		}

		.icon-shouhuodizhi {
			font-size: 36upx;
			color: $font-color-light;
		}
	}

	.default-row {
		margin-top: 16upx;

		.tit {
			flex: 1;
		}

		switch {
			transform: translateX(16upx) scale(.9);
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}

	.normal-tag {
		font-size: 1em;
		color: $font-color-dark;
		margin-right: 1em;
		background: #f5f3f5;
		border: 1px solid #f5f3f5;
		border-radius: 12rpx;
		padding-left: 1em;
		padding-right: 1em;
		padding-top: 0.5em;
		padding-bottom: 0.5em;
		line-height: 1;
	}

	.choose-tag {
		color: #fa436a;
		border: 1px solid #fa436a;
	}
</style>